package main.java.middle;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @Author: 郑国荣
 * @Date: 2023-08-21-14:38
 * @Description:
 */
public class RemoveDuplicates {

    public static int removeDuplicates(int[] nums){
        HashMap<Integer, Integer> map = new HashMap<>();
        int len = nums.length;
        int result =len;
        int left=0,right=0;
        while (right<len){
            if (map.containsKey(nums[right])){
                int count = map.get(nums[right]);
                if (count<2){
                    nums[left++]=nums[right];
                    map.replace(nums[right],++count);
                } else result--;
            } else {
                map.put(nums[right],1);
                nums[left++] = nums[right];
            }
            right++;
        }
        return result;
    }
    public static int remove2(int[] nums){
        int u = 0,k=2;

        for (int x : nums) {
            if (u < k || nums[u - k] != x) nums[u++] = x;
        }
        return u;


    }


    public static void main(String[] args) {
        int[] nums = new int[]{1,2,2,2,2,3,4};
        int len = remove2(nums);
//        System.out.println(Arrays.toString(nums));
        for (int i = 0; i < len; i++) {
            System.out.println(nums[i]);
        }
    }
}
